#ifndef _FLASH_CONTROLLER_H_
#define _FLASH_CONTROLLER_H_

#include "types.h"
#include "err.h"
#include "spi_controller.h"
#include "semaphores.h"

#define FLASH_BASE SPI1  // Flash connected to SPI-0
#define FLASH_CONFIG_MEM "spi1_flash"

typedef struct {

    sem_t    flash_lock;  // lock to access flash
    sem_t    client_lock;  // lock to wait for flash operation to complete

    uint16_t transmit_count;
    uint16_t receive_count;

    uint16_t  bytes_transmitted;
    uint16_t  bytes_received;

    uint8_t *transmit_buffer;
    uint8_t *receive_buffer;

    bool ignore_bytes;  // Skip initial bytes till command is complete

}flash_config_t;

typedef struct {
    uint8_t tx_header[10];  // Used by driver internally to send tx command
    uint8_t tx_buffer[0];   // Data passed in by the user
}flash_tx_buffer_t;

err_t flash_init(void);
void dump_flash_regs(void);
void test_flash(void);
err_t flash_read_data(flash_config_t *flash_config, uint32_t addr, 
         uint16_t bytes, uint8_t *buffer);

#endif
